﻿using SuporeSliplampApp.Models;
using SuporeSliplampApp.Util;
using SuporeSliplampApp.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using SuporeSliplampApp.Base;

namespace SuporeSliplampApp.ViewModels
{
    public class HomeViewModel
    {
        static public HomeViewModel homeViewModel = new HomeViewModel();
        public static HomeViewModel GetInstance()
        {
            return homeViewModel;
        }
        public HomeModel HomeModel { get; set; } = new HomeModel();

        private CommandBase _menuItemCommand;
        public CommandBase MenuItemCommand
        {
            get
            {
                if (_menuItemCommand == null)
                {
                    _menuItemCommand = new CommandBase();
                    _menuItemCommand.DoExecute = new Action<object>(obj =>
                    {
                        NavPage(obj.ToString());                        
                    });
                }
                return _menuItemCommand;
            }

        }

        private CommandBase _maxCommand;
        public CommandBase MaxCommand
        {
            get
            {
                if (_maxCommand == null)
                {
                    _maxCommand = new CommandBase();
                    _maxCommand.DoExecute = new Action<object>(obj =>
                    {
                        test(obj);
                    });
                }
                return _menuItemCommand;
            }

        }

        private CommandBase _addPatientCommand;
        public CommandBase AddPatientCommand
        {
            get
            {
                if (_addPatientCommand == null)
                {
                    _addPatientCommand = new CommandBase();
                    _addPatientCommand.DoExecute = new Action<object>(obj =>
                    {
                        addPatient();
                    });
                }
                return _addPatientCommand;
            }

        }

        private CommandBase _openPatientCommand;
        public CommandBase OpenPatientCommand
        {
            get
            {
                if (_openPatientCommand == null)
                {
                    _openPatientCommand = new CommandBase();
                    _openPatientCommand.DoExecute = new Action<object>(obj =>
                    {
                        OpenPatient();
                    });
                }
                return _openPatientCommand;
            }

        }

        private CommandBase _addUserCommand;
        public CommandBase AddUserCommand
        {
            get
            {
                if (_addUserCommand == null)
                {
                    _addUserCommand = new CommandBase();
                    _addUserCommand.DoExecute = new Action<object>(obj =>
                    {
                        AddUser();
                    });
                }
                return _addUserCommand;
            }

        }

        private void AddUser()
        {
            NewUserWindow newUserWindow = new NewUserWindow();
            newUserWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            newUserWindow.ShowDialog();
        }

        private CommandBase contextMenuCmd;

        public CommandBase ContextMenuCmd
        {
            get
            {
                if (contextMenuCmd == null)
                {
                    contextMenuCmd = new CommandBase();
                    contextMenuCmd.DoExecute = new Action<object>(obj =>
                    {
                        System.Windows.Controls.Button button = obj as System.Windows.Controls.Button;
                        System.Windows.Controls.ContextMenu contextMenu = button.ContextMenu;
                        contextMenu.PlacementTarget = button;
                        contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
                        contextMenu.IsOpen = true;

                    });
                }
                return contextMenuCmd;
            }

        }

        private void addPatient()
        {
            NewPatientWindow newPatientWindow = new NewPatientWindow();
            newPatientWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            newPatientWindow.ShowDialog();
        }

        private void OpenPatient()
        {
            OpenPatients openPatient = new OpenPatients();
            openPatient.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            openPatient.ShowDialog();
        }


        public void test(object str)
        {
            var t = str as ContentControl;
            t.Width = 1920;
            t.Height = 1080;
        }

        private CommandBase _homeCommand;

        public CommandBase HomeCommand
        {
            get
            {
                _homeCommand = new CommandBase();
                _homeCommand.DoExecute = new Action<object>(obj => { this.NavPage("SuporeSliplampApp.Views.MainContent"); });

                return this._homeCommand;
            }

        }

        private CommandBase _checkCmd;
        public CommandBase CheckCmd
        {
            get
            {
                if (_checkCmd == null)
                {
                    _checkCmd = new CommandBase();
                    _checkCmd.DoExecute = new Action<object>(obj =>
                    {
                        if ((bool)obj)
                        {
                            System.Windows.MessageBox.Show("test");
                        }
                    });
                }
                return _checkCmd;
            }
        }

        private CommandBase _openCameraCommand;

        public CommandBase OpenCameraCommand
        {
            get
            {
                if (_openCameraCommand == null)
                {
                    _openCameraCommand = new CommandBase();
                    _openCameraCommand.DoExecute = new Action<object>(obj =>
                    {
                        Task.Run(() =>
                        {
                            this.HomeModel.LableMsg = "正在打开相机.....";
                        });
                    });
                }
                return _openCameraCommand;
            }
        }

        private void test2(string name)
        {
            Type type = Type.GetType(name);

        }

        public void NavPage(string name)
        {
            Type type = Type.GetType(name);
            this.HomeModel.MainContent = (System.Windows.UIElement)Activator.CreateInstance(type);
            this.HomeModel.MainContent.Visibility = Visibility.Visible;

        }

        public void showWindow(string name)
        {
            Type type = Type.GetType(name);
            Window window = (Window)Activator.CreateInstance(type);
            window.Show();
        }

        public HomeViewModel()
        {
            this.NavPage("SuporeSliplampApp.Views.MainView");                  
        }
    }
}
